<template>
  <div class="container">
    <div class="hd">
      <el-button type="success" size="small" @click="toOrder">预约</el-button>
    </div>
    <!--    表格区域-->
    <div
      class="table"
      style="height: calc(100% - 160px); margin-top: 20px; overflow-y: auto"
    >
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        :header-cell-style="{ background: '#FEF6F6' }"
      >
        <el-table-column prop="canteenName" label="食堂" width="200">
        </el-table-column>
        <el-table-column prop="messageName" label="包房名" width="200">
        </el-table-column>
        <el-table-column prop="orderEatTime" label="时间" width="200">
        </el-table-column>
        <el-table-column prop="orderEatType" label="类型" width="200">
          <template #default="scope">
            <span v-show="scope.row.orderEatType === '0'">午餐</span>
            <span v-show="scope.row.orderEatType === '1'">晚餐</span>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="餐标(元)" width="200">
        </el-table-column>
        <el-table-column prop="orderIstrue" label="进度" width="200">
          <template #default="scope">
            <el-tag
              type="primary"
              size="small"
              v-show="scope.row.orderIstrue === '-1'"
              >未审核</el-tag
            >
            <el-tag
              type="danger"
              size="small"
              v-show="scope.row.orderIstrue === '0'"
              >未通过</el-tag
            >
            <el-tag
              type="success"
              size="small"
              v-show="scope.row.orderIstrue === '1'"
              >已通过</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="240px">
          <template #default="scope">
            <el-button
              style="color: #3cb6e6"
              type="text"
              size="mini"
              @click="details(scope.row.orderId)"
              >详情</el-button
            >
            <el-button
              style="color: #f56c6c"
              type="text"
              size="mini"
              v-show="scope.row.orderIstrue === '-1'"
              @click="cancel(scope.row.orderId)"
              >撤回</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    分页区域-->
    <el-pagination
      style="float: right; margin: 20px"
      background
      v-model:currentPage="pageNum"
      v-model:page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
    />
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { orderList, cancelOrder } from "@/api/canteen/order";
import commonPageRepository from "@/composables/common-page-repository";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
//表格数据
const tableData = ref([]);
//分页查询数据
const pageNum = ref(1);
const pageSize = ref(100);
const totalNum = ref(0);
//获取分页数据
function getOrderList() {
  orderList({
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }).then((res) => {
    tableData.value = res.data.content;
    totalNum.value = res.data.totalElements;
  });
}
//改变分页事件
const { pageSizeChange, pageCurrentChange } = commonPageRepository(
  pageSize,
  pageNum,
  getOrderList
);
//跳转到我要预约
const router = useRouter();
function toOrder() {
  router.push({
    name: "orderRoom",
  });
}
//跳转到预约详情页面
function details(id) {
  console.log(id);
  router.push({
    name: "orderDetail",
    query: {
      id,
    },
  });
}
//撤回订单
function cancel(id) {
  ElMessageBox.confirm("您确定要取消预约?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      cancelOrder(id).then((res) => {
        if (res.code === 200) {
          ElMessage.success("取消成功");
          getOrderList();
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
  console.log(id);
}
onMounted(() => {
  getOrderList();
});
</script>

<style scoped lang="scss">
.container {
  height: calc(100vh - 200px);
  background: #ffffff;
  box-shadow: 0 0 16px rgba(96, 96, 96, 0.16);
  border-radius: 6px;
  padding: 20px;
  overflow-y: auto;
  margin-top: 20px;
  .hd {
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
</style>
